package com.xunker.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunker.appointment.enums.OrderStatusEnum;
import com.xunker.appointment.model.orderinfo.OrderInfo;
import com.xunker.appointment.result.Result;
import com.xunker.appointment.vo.order.OrderCountQueryVo;
import com.xunker.appointment.vo.order.OrderQueryVo;
import com.xunker.order.service.OrderService;
import com.xunker.utils.AuthContextHolder;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result submitOrder(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable String scheduleId,
            @ApiParam(name = "patientId", value = "就诊人id", required = true)
            @PathVariable String patientId
    ) {
        Long isSave=orderService.saveOrder(scheduleId, patientId);
        return Result.ok(isSave);
    }
    //根据订单id查询订单详情
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable("orderId") String orderId) {
        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderInfo);
    }
    //订单列表查询
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit, OrderQueryVo orderQueryVo, HttpServletRequest request){
        //设置当前用户id
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> objectPage = new Page<>(page, limit);
        IPage<OrderInfo> iPage=orderService.getPage(objectPage,orderQueryVo);
        return Result.ok(iPage);
    }
    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }
    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable("orderId") Long orderId) {
        return Result.ok(orderService.cancelOrder(orderId));
    }
    /**
     * 获取订单统计数据
     */
    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo){
        return orderService.getCountMap(orderCountQueryVo);
    }

}
